Conditions | 21 |
Total Lines | 51 |
Code Lines | 35 |
Lines | 51 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like demo.js ➔ load_sbAdmin2Js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | View Code Duplication | app.run(function($rootScope, $transitions, $timeout, $q, $trace){ |
|
80 | function load_sbAdmin2Js(transition){ |
||
81 | var toID = transition.$id; |
||
82 | var toName = transition.to().name; |
||
83 | var childName = toName.split("."); |
||
84 | |||
85 | // initial the menu after the ng-include finished |
||
86 | $('#side-menu').metisMenu('dispose'); |
||
87 | $('ul.nav a[class="active"]').removeClass("active") |
||
88 | $('#side-menu').metisMenu(); |
||
89 | |||
90 | $(window).bind("load resize", function() { |
||
91 | var topOffset = 50; |
||
92 | var width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width; |
||
93 | if (width < 768) { |
||
94 | $('div.navbar-collapse').addClass('collapse'); |
||
95 | topOffset = 100; // 2-row-menu |
||
96 | } else { |
||
97 | $('div.navbar-collapse').removeClass('collapse'); |
||
98 | } |
||
99 | |||
100 | var height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1; |
||
101 | height = height - topOffset; |
||
102 | if (height < 1) height = 1; |
||
103 | if (height > topOffset) { |
||
104 | $("#page-wrapper").css("min-height", (height) + "px"); |
||
105 | } |
||
106 | }); |
||
107 | |||
108 | // keithpoon, 20180221, remove all class for single page application |
||
109 | var element = $('ul.nav a[ui-sref]').filter(function() { |
||
110 | var uiSref = $(this).attr('ui-sref') |
||
111 | |||
112 | if(toName == uiSref) |
||
113 | return true; |
||
114 | if(childName.length>0) |
||
115 | { |
||
116 | if(uiSref.indexOf('.') == 0) |
||
117 | return uiSref.substring(1) == childName[childName.length-1]; |
||
118 | } |
||
119 | |||
120 | return false; |
||
121 | }).addClass('active').parent(); |
||
122 | |||
123 | while (true) { |
||
124 | if (element.is('li')) { |
||
125 | element = element.parent().addClass('in').parent(); |
||
126 | } else { |
||
127 | break; |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | }); |